/*
 * SyncTarget.java
 *
 * Created on April 11, 2007, 8:42 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.atomojo.app.db;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.logging.Level;
import org.atomojo.app.InfosetRepresentation;
import org.atomojo.app.admin.AdminXML;
import org.infoset.xml.Element;
import org.infoset.xml.InfosetFactory;
import org.infoset.xml.XMLException;
import org.infoset.xml.util.XMLWriter;
import org.restlet.data.MediaType;
import org.restlet.representation.FileRepresentation;
import org.restlet.representation.Representation;

/**
 *
 * @author alex
 */
public class SyncTarget extends XMLObject implements StoredXML
{
   DB db;
   String name;
   String path;
   
   public SyncTarget(DB db,Representation rep)
   {
      super(rep);
      this.db = db;
   }
   
   /** Creates a new instance of SyncTarget */
   public SyncTarget(DB db,Element xml)
   {
      super(xml);
      this.name = xml.getAttributeValue("name");
      this.path = null;
      this.db = db;
   }
   
   /** Creates a new instance of SyncTarget */
   public SyncTarget(DB db,String name)
   {
      super((Element)null);
      this.name = name;
      this.path = null;
      this.db = db;
   }
   
   public SyncTarget(DB db,String name,String path)
   {
      super((Element)null);
      this.name = name;
      this.path = path;
      this.db = db;
   }
   
   public String getName() {
      return name;
   }
   
   public String getPath() {
      return path;
   }
   
   public Representation getRepresentation() {
      if (rep==null && name!=null) {
         
         rep = new FileRepresentation(getFile(),MediaType.APPLICATION_XML);
      }
      return super.getRepresentation();
   }
   
   protected File getFile() {
      return new File(db.getSyncTargetsDir(),name+".xml");
   }
   
   public void unmarshall()
      throws XMLException
   {
      if (xml==null) {
         try {
            parse();
         } catch (IOException ex) {
            throw new XMLException("Cannot load XML due to I/O exception.",ex);
         }
      }
      Element top = getElement();
      name = top.getAttributeValue("name");
      path = top.getAttributeValue("path");
   }
   
   public void marshall()
      throws XMLException
   {
      xml = InfosetFactory.getDefaultInfoset().createItemConstructor().createDocument().createDocumentElement(AdminXML.NM_TARGET);
      xml.setAttributeValue("name",name);
      xml.setAttributeValue("path",path);
      rep = new InfosetRepresentation(MediaType.APPLICATION_XML,xml.getDocument());
   }
   
   public boolean exists()
   {
      return getFile().exists();
   }
   
   public boolean create()
   {
      return update();
   }
   
   public boolean delete()
   {
      return getFile().delete();
   }
   
   public boolean update()
   {
      try {
         Writer out = new OutputStreamWriter(new FileOutputStream(getFile()),"UTF-8");
         XMLWriter.writeDocument(xml.getDocument(), out);
         out.flush();
         out.close();
         return true;
      } catch (Exception ex) {
         db.getLogger().log(Level.SEVERE,"Cannot store target to "+getFile()+" due to exception.",ex);
         return false;
      }
   }
   
}
